package exception.test04;

public class MyStack {
    Object[] elements;
    int index;
    public MyStack(Object[] elements){
        this.elements = elements;
    }

    public MyStack() {
        this.elements = new Object[10];
    }

    public void push(Object[] num) throws MyStackOperationException {
        if(index + num.length > 100){
            throw new MyStackOperationException("压栈失败，栈已满！");
        }else if(index + num.length > 99){
            System.out.println("栈还有一个空间");
        }
        System.arraycopy(num,0,elements,index,num.length);
        this.index = index + num.length;
        System.out.println("已压入 " + num.length + " 个元素," + "栈中已有 " + this.index + " 个元素");
    }

    public void pop(int num){
        if(index - num == 0){
            System.out.println("栈已空");
            return;
        }else if(index - num == 1){
            System.out.println("栈只有一个空间了");
        }else if(index - num < 0){
            System.arraycopy(new Object[index],0,elements,0,index);
            System.out.println("栈已空,只能释放 " + index + " 个元素," + "栈中已有 " + 0 + " 个元素");
            this.index = 0;
            return;
        }
        System.arraycopy(new Object[num],0,elements,index - num,num);
        this.index = index - num;
        System.out.println("已释放 " + num + " 个元素," + "栈中已有 " + this.index + " 个元素");
    }
}
